﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YJKRevitBase.Math;

namespace Assist
{
	public class LinkCurve
	{
		public LinkCurve()
		{
		}

		public LinkCurve(List<Curve> curveList)
		{
			this.CurveList = curveList;
		}

		public void Add(Curve curve)
		{
			this.CurveList.Add(curve);
		}

		public double Length
		{
			get
			{
				double num = 0.0;
				foreach (Curve curve in this.CurveList)
				{
					num += curve.Length;
				}
				return num;
			}
		}

		public int Count
		{
			get
			{
				return this.CurveList.Count;
			}
		}

		public List<XYZ> Tessellate
		{
			get
			{
				List<XYZ> list = new List<XYZ>();
				foreach (Curve curve in this.CurveList)
				{
					list.AddRange(curve.Tessellate());
				}
				return list;
			}
		}

		private double GetAngle(XYZ ptOut)
		{
			XYZ xyz = ptOut - XYZ.Zero;
			double angle = XYZ.BasisX.AngleOnPlaneTo(xyz, XYZ.BasisZ);
			return MathUtility.formatAngle(angle);
		}

		public List<IntersectionPoint> Divide(double num, UIApplication uiApp)
		{
			double num2 = this.Length / num;
			this.Sort(uiApp);
			List<IntersectionPoint> list = new List<IntersectionPoint>();
			double num3 = 0.0;
			foreach (Curve curve in this.CurveList)
			{
				bool flag = true;
				bool isBound = curve.IsBound;
				if (isBound)
				{
					XYZ xyz = curve.GetEndPoint(0);
					while (flag)
					{
						bool flag2 = curve is Line;
						if (flag2)
						{
							Transform transform = curve.ComputeDerivatives(1.0, true);
							XYZ ptOut = transform.get_Basis(0);
							double angle = this.GetAngle(ptOut);
							Arc arc = uiApp.ActiveUIDocument.Document.CreatYJKArc(xyz, Math.Abs(num2 - num3), angle - 1.0, angle + 1.0, XYZ.BasisX, XYZ.BasisY);
							IntersectionResultArray intersectionResultArray = new IntersectionResultArray();
							curve.Intersect(arc, out intersectionResultArray);
							bool flag3 = intersectionResultArray != null;
							if (flag3)
							{
								IEnumerator enumerator2 = intersectionResultArray.GetEnumerator();
								{
									if (enumerator2.MoveNext())
									{
										IntersectionResult intersectionResult = (IntersectionResult)enumerator2.Current;
										xyz = intersectionResult.XYZPoint;
										list.Add(new IntersectionPoint(xyz, this.GetAngle(ptOut)));
										num3 = 0.0;
									}
								}
							}
							else
							{
								flag = false;
								try
								{
									num3 += uiApp.Application.CreatYJKLine(xyz, curve.GetEndPoint(1), true).Length;
								}
								catch
								{
								}
							}
						}
						else
						{
							bool flag4 = curve is Arc;
							if (flag4)
							{
								double hyendParameter = curve.GetYJKEndParameter(0);
								double hyendParameter2 = curve.GetYJKEndParameter(1);
								double num4 = hyendParameter;
								while (flag)
								{
									double num5 = num4 + (hyendParameter2 - hyendParameter) * (num2 - num3) / curve.Length;
									bool flag5 = curve.IsInside(num5);
									if (flag5)
									{
										double num6 = curve.ComputeNormalizedParameter(num5);
										XYZ pt = curve.Evaluate(num6, true);
										Transform transform2 = curve.ComputeDerivatives(num5, false);
										XYZ ptOut2 = transform2.get_Basis(0);
										list.Add(new IntersectionPoint(pt, this.GetAngle(ptOut2)));
										double num7 = curve.Length * (hyendParameter2 - num5) / (hyendParameter2 - hyendParameter);
										bool flag6 = num7 < num2;
										if (flag6)
										{
											flag = false;
											num3 = curve.Length * (hyendParameter2 - num5) / (hyendParameter2 - hyendParameter);
										}
										else
										{
											num4 = num5;
											num3 = 0.0;
										}
									}
									else
									{
										flag = false;
										num3 = curve.Length * (hyendParameter2 - num5) / (hyendParameter2 - hyendParameter);
									}
								}
							}
							else
							{
								double hyendParameter3 = curve.GetYJKEndParameter(0);
								double hyendParameter4 = curve.GetYJKEndParameter(1);
								IList<XYZ> list2 = curve.Tessellate();
								double num8 = 0.0;
								double num9 = 0.0;
								XYZ xyz2 = curve.GetEndPoint(0);
								foreach (XYZ xyz3 in list2)
								{
									try
									{
										num8 += xyz2.DistanceTo(xyz3);
										num9 += xyz2.DistanceTo(xyz3);
										bool flag7 = num8 >= num2 - num3;
										if (flag7)
										{
											XYZ ptOut3 = xyz3 - xyz2;
											list.Add(new IntersectionPoint(xyz3, this.GetAngle(ptOut3)));
											num8 = 0.0;
											num3 = 0.0;
										}
										xyz2 = xyz3;
									}
									catch (Exception ex)
									{
										MessageBox.Show(ex.Message);
									}
								}
								bool flag8 = list.Count > 0;
								if (flag8)
								{
									IntersectionPoint intersectionPoint = list[list.Count - 1];
									bool flag9 = false;
									foreach (XYZ xyz4 in list2)
									{
										bool flag10 = xyz4.IsAlmostEqualTo(intersectionPoint.Point);
										if (flag10)
										{
											flag9 = true;
										}
										bool flag11 = flag9;
										if (flag11)
										{
											num3 += xyz2.DistanceTo(xyz4);
										}
										xyz2 = xyz4;
									}
								}
								else
								{
									num3 += curve.Length;
								}
								flag = false;
							}
						}
					}
				}
				else
				{
					bool flag12 = curve is Arc;
					if (flag12)
					{
						double hyendParameter5 = curve.GetYJKEndParameter(0);
						double hyendParameter6 = curve.GetYJKEndParameter(1);
						double num10 = hyendParameter5;
						while (flag)
						{
							double num11 = num10 + (hyendParameter6 - hyendParameter5) * (num2 - num3) / curve.Length;
							bool flag13 = curve.IsInside(num11);
							if (flag13)
							{
								double num12 = curve.ComputeNormalizedParameter(num11);
								XYZ pt2 = curve.Evaluate(num12, true);
								Transform transform3 = curve.ComputeDerivatives(num11, false);
								XYZ ptOut4 = transform3.get_Basis(0);
								list.Add(new IntersectionPoint(pt2, this.GetAngle(ptOut4)));
								double num13 = curve.Length * (hyendParameter6 - num11) / (hyendParameter6 - hyendParameter5);
								bool flag14 = num13 < num2;
								if (flag14)
								{
									flag = false;
									num3 = curve.Length * (hyendParameter6 - num11) / (hyendParameter6 - hyendParameter5);
								}
								else
								{
									num10 = num11;
									num3 = 0.0;
								}
							}
							else
							{
								flag = false;
								num3 = curve.Length * (hyendParameter6 - num11) / (hyendParameter6 - hyendParameter5);
							}
						}
					}
					else
					{
						double hyendParameter7 = curve.GetYJKEndParameter(0);
						double hyendParameter8 = curve.GetYJKEndParameter(1);
						IList<XYZ> list3 = curve.Tessellate();
						double num14 = 0.0;
						double num15 = 0.0;
						XYZ xyz5 = curve.GetEndPoint(0);
						foreach (XYZ xyz6 in list3)
						{
							try
							{
								num14 += xyz5.DistanceTo(xyz6);
								num15 += xyz5.DistanceTo(xyz6);
								bool flag15 = num14 >= num2 - num3;
								if (flag15)
								{
									XYZ ptOut5 = xyz6 - xyz5;
									list.Add(new IntersectionPoint(xyz6, this.GetAngle(ptOut5)));
									num14 = 0.0;
									num3 = 0.0;
								}
								xyz5 = xyz6;
							}
							catch (Exception ex2)
							{
								MessageBox.Show(ex2.Message);
							}
						}
						bool flag16 = list.Count > 0;
						if (flag16)
						{
							IntersectionPoint intersectionPoint2 = list[list.Count - 1];
							bool flag17 = false;
							foreach (XYZ xyz7 in list3)
							{
								bool flag18 = xyz7.IsAlmostEqualTo(intersectionPoint2.Point);
								if (flag18)
								{
									flag17 = true;
								}
								bool flag19 = flag17;
								if (flag19)
								{
									num3 += xyz5.DistanceTo(xyz7);
								}
								xyz5 = xyz7;
							}
						}
						else
						{
							num3 += curve.Length;
						}
						flag = false;
					}
				}
			}
			bool flag20 = (double)list.Count > num - 1.0;
			if (flag20)
			{
				IntersectionPoint item = list[list.Count - 1];
				list.Remove(item);
			}
			return list;
		}

		public List<IntersectionPoint> Liner(double length, UIApplication uiApp)
		{
			double num = Math.Ceiling(this.Length / length);
			this.Sort(uiApp);
			List<IntersectionPoint> list = new List<IntersectionPoint>();
			double num2 = 0.0;
			foreach (Curve curve in this.CurveList)
			{
				bool flag = true;
				bool isBound = curve.IsBound;
				if (isBound)
				{
					XYZ xyz = curve.GetEndPoint(0);
					while (flag)
					{
						bool flag2 = curve is Line;
						if (flag2)
						{
							Transform transform = curve.ComputeDerivatives(1.0, true);
							XYZ ptOut = transform.get_Basis(0);
							double angle = this.GetAngle(ptOut);
							Arc arc = uiApp.ActiveUIDocument.Document.CreatYJKArc(xyz, Math.Abs(length - num2), angle - 1.0, angle + 1.0, XYZ.BasisX, XYZ.BasisY);
							IntersectionResultArray intersectionResultArray = new IntersectionResultArray();
							curve.Intersect(arc, out intersectionResultArray);
							bool flag3 = intersectionResultArray != null;
							if (flag3)
							{
								IEnumerator enumerator2 = intersectionResultArray.GetEnumerator();
								{
									if (enumerator2.MoveNext())
									{
										IntersectionResult intersectionResult = (IntersectionResult)enumerator2.Current;
										xyz = intersectionResult.XYZPoint;
										list.Add(new IntersectionPoint(xyz, this.GetAngle(ptOut)));
										num2 = 0.0;
									}
								}
							}
							else
							{
								flag = false;
								try
								{
									num2 += uiApp.Application.CreatYJKLine(xyz, curve.GetEndPoint(1), true).Length;
								}
								catch
								{
								}
							}
						}
						else
						{
							bool flag4 = curve is Arc;
							if (flag4)
							{
								double hyendParameter = curve.GetYJKEndParameter(0);
								double hyendParameter2 = curve.GetYJKEndParameter(1);
								double num3 = hyendParameter;
								while (flag)
								{
									double num4 = num3 + (hyendParameter2 - hyendParameter) * (length - num2) / curve.Length;
									bool flag5 = curve.IsInside(num4);
									if (flag5)
									{
										double num5 = curve.ComputeNormalizedParameter(num4);
										XYZ pt = curve.Evaluate(num5, true);
										Transform transform2 = curve.ComputeDerivatives(num4, false);
										XYZ ptOut2 = transform2.get_Basis(0);
										list.Add(new IntersectionPoint(pt, this.GetAngle(ptOut2)));
										double num6 = curve.Length * (hyendParameter2 - num4) / (hyendParameter2 - hyendParameter);
										bool flag6 = num6 < length;
										if (flag6)
										{
											flag = false;
											num2 = curve.Length * (hyendParameter2 - num4) / (hyendParameter2 - hyendParameter);
										}
										else
										{
											num3 = num4;
											num2 = 0.0;
										}
									}
									else
									{
										flag = false;
										num2 = curve.Length * (hyendParameter2 - num4) / (hyendParameter2 - hyendParameter);
									}
								}
							}
							else
							{
								double hyendParameter3 = curve.GetYJKEndParameter(0);
								double hyendParameter4 = curve.GetYJKEndParameter(1);
								IList<XYZ> list2 = curve.Tessellate();
								double num7 = 0.0;
								double num8 = 0.0;
								XYZ xyz2 = curve.GetEndPoint(0);
								foreach (XYZ xyz3 in list2)
								{
									try
									{
										num7 += xyz2.DistanceTo(xyz3);
										num8 += xyz2.DistanceTo(xyz3);
										bool flag7 = num7 >= length - num2;
										if (flag7)
										{
											XYZ ptOut3 = xyz3 - xyz2;
											list.Add(new IntersectionPoint(xyz3, this.GetAngle(ptOut3)));
											num7 = 0.0;
											num2 = 0.0;
										}
										xyz2 = xyz3;
									}
									catch (Exception ex)
									{
										MessageBox.Show(ex.Message);
									}
								}
								bool flag8 = list.Count > 0;
								if (flag8)
								{
									IntersectionPoint intersectionPoint = list[list.Count - 1];
									bool flag9 = false;
									foreach (XYZ xyz4 in list2)
									{
										bool flag10 = xyz4.IsAlmostEqualTo(intersectionPoint.Point);
										if (flag10)
										{
											flag9 = true;
										}
										bool flag11 = flag9;
										if (flag11)
										{
											num2 += xyz2.DistanceTo(xyz4);
										}
										xyz2 = xyz4;
									}
								}
								else
								{
									num2 += curve.Length;
								}
								flag = false;
							}
						}
					}
				}
				else
				{
					bool flag12 = curve is Arc;
					if (flag12)
					{
						double hyendParameter5 = curve.GetYJKEndParameter(0);
						double hyendParameter6 = curve.GetYJKEndParameter(1);
						double num9 = hyendParameter5;
						while (flag)
						{
							double num10 = num9 + (hyendParameter6 - hyendParameter5) * (length - num2) / curve.Length;
							bool flag13 = curve.IsInside(num10);
							if (flag13)
							{
								double num11 = curve.ComputeNormalizedParameter(num10);
								XYZ pt2 = curve.Evaluate(num11, true);
								Transform transform3 = curve.ComputeDerivatives(num10, false);
								XYZ ptOut4 = transform3.get_Basis(0);
								list.Add(new IntersectionPoint(pt2, this.GetAngle(ptOut4)));
								double num12 = curve.Length * (hyendParameter6 - num10) / (hyendParameter6 - hyendParameter5);
								bool flag14 = num12 < length;
								if (flag14)
								{
									flag = false;
									num2 = curve.Length * (hyendParameter6 - num10) / (hyendParameter6 - hyendParameter5);
								}
								else
								{
									num9 = num10;
									num2 = 0.0;
								}
							}
							else
							{
								flag = false;
								num2 = curve.Length * (hyendParameter6 - num10) / (hyendParameter6 - hyendParameter5);
							}
						}
					}
					else
					{
						double hyendParameter7 = curve.GetYJKEndParameter(0);
						double hyendParameter8 = curve.GetYJKEndParameter(1);
						IList<XYZ> list3 = curve.Tessellate();
						double num13 = 0.0;
						double num14 = 0.0;
						XYZ xyz5 = curve.GetEndPoint(0);
						foreach (XYZ xyz6 in list3)
						{
							try
							{
								num13 += xyz5.DistanceTo(xyz6);
								num14 += xyz5.DistanceTo(xyz6);
								bool flag15 = num13 >= length - num2;
								if (flag15)
								{
									XYZ ptOut5 = xyz6 - xyz5;
									list.Add(new IntersectionPoint(xyz6, this.GetAngle(ptOut5)));
									num13 = 0.0;
									num2 = 0.0;
								}
								xyz5 = xyz6;
							}
							catch (Exception ex2)
							{
								MessageBox.Show(ex2.Message);
							}
						}
						bool flag16 = list.Count > 0;
						if (flag16)
						{
							IntersectionPoint intersectionPoint2 = list[list.Count - 1];
							bool flag17 = false;
							foreach (XYZ xyz7 in list3)
							{
								bool flag18 = xyz7.IsAlmostEqualTo(intersectionPoint2.Point);
								if (flag18)
								{
									flag17 = true;
								}
								bool flag19 = flag17;
								if (flag19)
								{
									num2 += xyz5.DistanceTo(xyz7);
								}
								xyz5 = xyz7;
							}
						}
						else
						{
							num2 += curve.Length;
						}
						flag = false;
					}
				}
			}
			bool flag20 = (double)list.Count > num - 1.0;
			if (flag20)
			{
				IntersectionPoint item = list[list.Count - 1];
				list.Remove(item);
			}
			return list;
		}

		public bool IsContains(UIApplication uiApp, Curve curve)
		{
			foreach (Curve c in this.CurveList)
			{
				bool flag = CurveAssistFunc.GetOverlapType(uiApp, curve, c) == OverlapType.eOverlap;
				if (flag)
				{
					return true;
				}
			}
			return false;
		}

		private Curve GetReverseCurve(Curve cur, UIApplication uiApp)
		{
			bool flag = cur is Line;
			Curve result;
			if (flag)
			{
				XYZ hyendPoint = cur.GetEndPoint(0);
				XYZ hyendPoint2 = cur.GetEndPoint(1);
				result = uiApp.Application.CreatYJKLine(hyendPoint2, hyendPoint, true);
			}
			else
			{
				bool flag2 = cur is Arc;
				if (flag2)
				{
					Arc arc = cur as Arc;
					XYZ hyendPoint3 = cur.GetEndPoint(0);
					XYZ hyendPoint4 = cur.GetEndPoint(1);
					XYZ radianPnt = arc.Tessellate()[1];
					XYZ center = arc.Center;
					result = uiApp.Application.CreatYJKArc(hyendPoint4, hyendPoint3, radianPnt);
				}
				else
				{
					result = cur;
				}
			}
			return result;
		}

		public void Sort(UIApplication uiApp)
		{
			bool flag = this.CurveList.Count > 1;
			if (flag)
			{
				List<XYZ> list = new List<XYZ>();
				List<Curve> list2 = new List<Curve>();
				foreach (Curve curve in this.CurveList)
				{
					list.Clear();
					foreach (Curve curve2 in this.CurveList)
					{
						bool flag2 = CurveAssistFunc.GetOverlapType(uiApp, curve, curve2) != OverlapType.eOverlap;
						if (flag2)
						{
							IntersectionResultArray intersectionResultArray = new IntersectionResultArray();
							curve.Intersect(curve2, out intersectionResultArray);
							bool flag3 = intersectionResultArray != null && intersectionResultArray.Size == 1;
							if (flag3)
							{
								list.Add(intersectionResultArray.get_Item(0).XYZPoint);
							}
						}
					}
					bool flag4 = list.Count == 1 && list[0].IsAlmostEqualTo(curve.GetEndPoint(1));
					if (flag4)
					{
						list2.Add(curve);
						break;
					}
				}
				bool flag5 = list2.Count <= 0;
				if (flag5)
				{
					list2.Add(this.CurveList[0]);
				}
				while (list2.Count < this.CurveList.Count)
				{
					Curve curve3 = list2[list2.Count - 1];
					foreach (Curve curve4 in this.CurveList)
					{
						bool flag6 = CurveAssistFunc.GetOverlapType(uiApp, curve3, curve4) != OverlapType.eOverlap;
						if (flag6)
						{
							IntersectionResultArray intersectionResultArray2 = new IntersectionResultArray();
							curve3.Intersect(curve4, out intersectionResultArray2);
							bool flag7 = intersectionResultArray2 != null && intersectionResultArray2.Size > 0;
							if (flag7)
							{
								bool flag8 = intersectionResultArray2.get_Item(0).XYZPoint.IsAlmostEqualTo(curve3.GetEndPoint(1));
								if (flag8)
								{
									bool flag9 = intersectionResultArray2.get_Item(0).XYZPoint.IsAlmostEqualTo(curve4.GetEndPoint(0));
									if (flag9)
									{
										list2.Add(curve4);
									}
									else
									{
										list2.Add(this.GetReverseCurve(curve4, uiApp));
									}
								}
							}
						}
					}
				}
				this.CurveList = list2;
			}
			else
			{
				try
				{
					XYZ hyendPoint = this.CurveList[0].GetEndPoint(0);
				}
				catch
				{
					MessageBox.Show("目前不能在闭合环中阵列，请重新选择。");
				}
			}
		}

		public List<Curve> CurveList = new List<Curve>();
	}
}
